package com.example.demo.Dao;

import com.example.demo.bean.Goods;
import com.example.demo.service.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodsDaoImpl implements GoodsDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
  /*  public void add(User user) {
        jdbcTemplate.update("insert into guest(score,level,balance) values(?, ?, ?)",
                user.getName(),user.getAmount(),user.getPrice());
    }*/
    public void add(Goods goods) {
        jdbcTemplate.update("insert into bread(name,amount,price) values(?, ?, ?)",
                goods.getName(),goods.getAmount(),goods.getPrice());
    }

    @Override
    public void del(Long id) {
        jdbcTemplate.update("delete from bread where id=?", id);

    }


    @Override
   /* public int update(User user) {
        return (int)jdbcTemplate.update("update guest set score=?,level=?,level=? where id=?",
                user.getName(),user.getAmount(),user.getPrice(),user.getId());
    }*/
    public int update(Goods goods) {
        return (int)jdbcTemplate.update("update bread set name=?,amount=?,price=? where id=?",
                goods.getName(),goods.getAmount(),goods.getPrice(),goods.getId());
    }

    @Override
    public List<Goods> findGoodsById(Long id) {
        List<Goods> userList = jdbcTemplate.query("select * from bread where id=?",new Object[]{id},new BeanPropertyRowMapper(Goods.class));
        if(userList!=null && userList.size()>0){
            return  userList;
          //   return  userList.get(0);
        }else{
            return null;
        }
    }

    @Override
    public boolean checkId(Long id) {
        List<Goods> userList = jdbcTemplate.query("select * from bread where id=?",new Object[]{id},new BeanPropertyRowMapper(Goods.class));
        if(userList==null || userList.size()==0){
            return  false;
            //   return  userList.get(0);
        }else{
            return true;
        }
    }

    @Override
    public List<Goods> findGoodsByName(String name) {
        List<Goods> userList = jdbcTemplate.query("select * from bread where name=?",new Object[]{name},new BeanPropertyRowMapper(Goods.class));
        if(userList!=null && userList.size()>0){
            return  userList;
            //   return  userList.get(0);
        }else{
            return null;
        }
    }


    @Override
    public List<Goods> findGoodsList() {
        List<Goods> userList = jdbcTemplate.query("select * from bread ",new Object[]{},new BeanPropertyRowMapper(Goods.class));
        if(userList!=null && userList.size()>0){
            return  userList;
        }else{
            return null;
        }
    }
}
